var express = require('express');
var router = express.Router();
var db = require('../public/javascripts/db');
var token = require('../public/javascripts/utils');
var config = require('../public/config')
const imgPath = require('../public/config').imgPath;

// const imgPath = 'http://localhost/upload/';  //本地环境

function getTime() {
    return new Date().toLocaleString('chinese', {
        hour12: false,
        year: "numeric",
        month: "2-digit",
        day: "2-digit",
        hour: "2-digit",
        minute: "2-digit",
        second: "2-digit"
    })
}

router.post('/', (req, res, next) => {
    console.log(req.body);
    var sql1 = "UPDATE images SET owner_id = ?,type = ? WHERE id = ?";
    var sql1Params = [req.body.owner_id, req.body.type, req.body.image_id];
    db.query(sql1, sql1Params, (err, result) => {
        if (err) {
            throw err;
            return;
        } else {
            console.log('update images', result);
        }
    })

    var sql2 = "UPDATE tasks SET status = ?,start_time = ? WHERE img_id = ?";
    var sql2Params = [2, getTime(), req.body.image_id];
    db.query(sql2, sql2Params, (err, result) => {
        if (err) {
            throw err;
            return;
        } else {
            console.log('insert into tasks', result);
        }
    })

    // var sql2 = "INSERT INTO `tasks` (img_id,user_id,owner_id,algorithm,status,start_time) VALUES (?,?,?,?,?,?)";
    // var sql2Params = [req.body.image_id, token.verifyToken(req.headers.token), req.body.owner_id, 1, req.body.status, getTime()];
    // db.query(sql2, sql2Params, (err, result) => {
    //     if (err) {
    //         throw err;
    //         return;
    //     } else {
    //         console.log('insert into tasks', result);
    //     }
    // })


    var exec = require('child_process').exec;
    var arg1 = req.body.image_url.split('/').pop();
    var filename = config.pythonPath;
    exec('python' + ' ' + filename + ' ' + arg1, (err, stdout, stderr) => {
        if (err) {
            console.log('python err', err);
            throw err;
            return;
        } else {
            // var end = new Date().getTime();
            var updateSql = 'UPDATE tasks SET status = ?,end_time = ? WHERE img_id = ?'
            var updateParams = [1, getTime(), req.body.image_id];
            db.query(updateSql, updateParams, (err1, res1) => {
                if (err1) {
                    throw err1;
                    return;
                } else {
                    var sql4 = 'create table if not exists results (' +
                        'id int not null auto_increment,' +
                        'img_id varchar(255),' +
                        'user_id varchar(255),' +
                        'owner_id varchar(255),' +
                        'isDr double,' +
                        'vessel varchar(255),' +
                        'mass_vessel varchar(255), odlocation varchar(255),created_time varchar(255),medical_num varchar(255), primary key (id))';
                    db.query(sql4, (err2, res2) => {
                        if (err2) {
                            throw err2;
                            return;
                        } else {
                            var add_sql4 = "INSERT INTO `results` (img_id,user_id,owner_id,isDr,vessel,mass_vessel,odlocation,created_time,medical_num) VALUES (?,?,?,?,?,?,?,?,?)";
                            var img_name = req.body.image_url.split('/').pop().split('.')[0];
                            var add_params4 = [req.body.image_id, token.verifyToken(req.headers.token), req.body.owner_id,
                                1.1, imgPath + img_name + '_vessel.jpg', imgPath + img_name + '_massvessel.jpg',
                                imgPath + img_name + '_odlocation.jpg', getTime(), req.body.medical_num];
                            db.query(add_sql4, add_params4, (err3, res3) => {
                                if (err3) {
                                    throw err3;
                                    return;
                                } else {
                                    console.log('done');
                                    res.send({status: 2})
                                    // res.send({status: 2})
                                }
                            })
                        }
                    })
                }
            })
        }
    })


})

module.exports = router;